package com.sfh.base;




import com.sfh.base.exception.HandleException;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * 功能描述:结果处理[需要把异常处理成自定义异常]
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/4/3
 */
public final class RxObserver<T> implements Consumer<T> ,Action {

	private Disposable disposable;
    private IResult<T> result;
    public RxObserver() {
      
    }
    public RxObserver(IResult<T> result) {
        this.result = result;
    }

    @Override
    public void accept(T o) throws Exception {
        if (this.result == null) {
            return;
        }
        this.result.onSuccess(o);
    }

    public Consumer<Throwable> onError =  new Consumer<Throwable>() {
        @Override
        public void accept(Throwable e) throws Exception {
     
            if (result == null) {
                return;
            }
            result.onFail(HandleException.handleException(e));
        }
    };

	@Override
	public void run() throws Exception {
		if (disposable != null) {
			disposable.dispose();
		}
		
	}

	public void setDisposable(Disposable disposable) {
		this.disposable = disposable;
	}

	

}
